import sys, re, operator

#anomaly = {}
#nonstable_alarm = {}
#stable_alarm = {}
prefixes = set()
hijack_file = open('./anomalies.format')
for l in hijack_file:
  c = l[:-1].split('|') # time, pfx, home, bad_path, is_hijk
  if c[2] != "0":
    #if c[4] == 'Y':
    #  if c[1] not in stable_alarm:
    #    stable_alarm[c[1]] = set()
    #  stable_alarm[c[1]].add('AS%s' % c[2])
    #elif c[4] == '6':
    #  if c[1] not in nonstable_alarm:
    #    nonstable_alarm[c[1]] = set()
    #  nonstable_alarm[c[1]].add('AS%s' % c[2])
    #else:
    #  if c[1] not in anomaly:
    #    anomaly[c[1]] = set()
    #  anomaly[c[1]].add('AS%s' % c[2])
    prefixes.add(c[1])
hijack_file.close()

import re
#radb_file = open('./radb.db')
radb_origins = {}
pfx = ''
for ln in sys.stdin:
  if ln.startswith('route:'):
    pfx_match = re.match('route:\s*([0-9\./]+)\s*$', ln)
    if not pfx_match:
      print >> sys.stderr, "%r" % ln
    pfx = pfx_match.group(1)
    if pfx not in prefixes:
      pfx = ''
  elif ln.startswith('origin:'):
    if pfx:
      st = radb_origins.get(pfx, None)
      if not st:
        st = set()
        radb_origins[pfx] = st
      ases_match = re.match('origin:\s*AS([0-9]+)\s*$', ln, re.IGNORECASE)
      if not ases_match:
        print >> sys.stderr, "%r" % ln
      else:
        AS = ases_match.group(1)
        st.add(AS)
  elif ln == '':
    pfx = ''
#radb_file.close()

for pfx, AS in radb_origins.iteritems():
  print pfx, ' '.join(AS)

#def get_whois(prefixes, mark):
#  global ENTRY_INDEX
#  sum_whois = 0
#  sum_match = 0
#  sum_one_whois = 0
#  sum_one_match = 0
#  for p, orig in prefixes.iteritems():
#    ENTRY_INDEX += 1
#    if ENTRY_INDEX <= LAST_END:
#      continue
#    try:
#      print "%s|%s|%s|" % (mark, p, ' '.join(orig)),
#      resp = commands.getoutput("%s%s" % (pfxurl, p))
#      #get_resp = get_req.get('%s%s' % (pfxurl, p), timeout=60)
#      #if get_resp and get_resp.text:
#      if resp:
#        lines = resp.split('\n')
#        has_origin = 0
#        match_origin = 0
#        for l in lines:
#          if l.startswith('origin:'):
#            c = l.split(' ')
#            for v in c:
#              if v.startswith('AS'):
#                has_origin += 1
#                if v in orig:
#                  match_origin += 1
#                  print '%s*' % v, 
#                else:
#                  print v, 
#        if has_origin:
#          sum_whois += len(orig)
#          sum_match += match_origin
#        if has_origin == 1:
#          sum_one_whois += len(orig)
#          sum_one_match += match_origin
#      print
#    except Exception as e:
#      print
#      print >> sys.stderr, "Error:", e
#  if sum_whois:
#    print >> sys.stderr, "all: %d %d %lf" % (
#        sum_match, sum_whois,
#        sum_match * 100. / sum_whois)
#  if sum_one_whois:
#    print >> sys.stderr, "one: %d %d %lf" % (
#        sum_one_match, sum_one_whois,
#        sum_one_match * 100. / sum_one_whois)
#
#print >> sys.stderr, "Stable alarms"
#get_whois(stable_alarm, 'Y')
#print >> sys.stderr, "Non-stable alarms"
#get_whois(nonstable_alarm, '6')
#print >> sys.stderr, "Anomalies"
#get_whois(anomaly, '-')

